import {
  config
} from "../../config"
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    locationList:[
      {
        id:1,
        isJoin:0,//0是未参加1是参加
      },
      {
        id:2,
        isJoin:0,//0是未参加1是参加
      },
      {
        id:3,
        isJoin:0,//0是未参加1是参加
      },
      {
        id:4,
        isJoin:0,//0是未参加1是参加
      },
      {
        id:5,
        isJoin:0,//0是未参加1是参加
      },
    ],
    locationImage:[
      {
        imageurl:'../../image/map/location-1.png',
        imageActive:'../../image/map/location-1-on.png'
      },
      {
        imageurl:'../../image/map/location-2.png',
        imageActive:'../../image/map/location-2-on.png'
      },
      {
        imageurl:'../../image/map/location-3.png',
        imageActive:'../../image/map/location-3-on.png'
      },
      {
        imageurl:'../../image/map/location-4.png',
        imageActive:'../../image/map/location-4-on.png'
      },
      {
        imageurl:'../../image/map/location-5.png',
        imageActive:'../../image/map/location-5-on.png'
      },
    ],
    lineList:[
      {
        id:1,
        imageurl:'../../image/map/line-1.png'
      },
      {
        id:2,
        imageurl:'../../image/map/line-2.png'
      },
      {
        id:3,
        imageurl:'../../image/map/line-3.png'
      },
      {
        id:4,
        imageurl:'../../image/map/line-4.png'
      },
    ],
    currentIndex: 0,//当前打卡到那个关卡
  },
  setNextBtn() {
    const listData = this.data.locationList
    const stateList = []
    listData.forEach((item) => {
      stateList.push(item.isJoin)
    })
    if (stateList.indexOf(0) === -1) {
      
    } else {
      this.setData({
        currentIndex: stateList.indexOf(0)
      })
      console.log(this.data.currentIndex)
    }
  },
  goScaneCode(event) { //扫码跳转
    const roundIndex = event.currentTarget.dataset.index
    console.log(roundIndex)
    if(this.data.locationList[roundIndex].isJoin===0){
         if(roundIndex===4){
          wx.navigateTo({
            url: '../answerQuestions/index?round=' + roundIndex
          })
        } else {
          wx.navigateTo({
            url: '../code/index?round=' + roundIndex
          })
        }
    }
    // if (roundIndex === this.data.currentIndex) {
    //   // wx.navigateTo({
    //   //   url: '../code/index?round=' + roundIndex
    //   // })
    //   console.log(this.data.currentIndex)
    //   if(this.data.currentIndex===4){
    //     wx.redirectTo({
    //       url: '../answerQuestions/index?round=' + roundIndex
    //     })
    //   } else {
    //     wx.redirectTo({
    //       url: '../code/index?round=' + roundIndex
    //     })
    //   }
    // }
  },
  getUserData() {
    const openId = app.globalData.openid
    //const openId = '1111cccssss'
    wx.request({
      url: `${config.api_blink_url}/Intel/g/user?openid=${openId}`,
      method: "get",
      header: {
        'content-type': 'application/json'
      },
      success: (res) => {
        const data = res.data.user
        const locationList = [{
            id: 1,
            isJoin: data.g1,
          },
          {
            id: 2,
            isJoin: data.g2,
          },
          {
            id: 3,
            isJoin: data.g3,
          },
          {
            id: 4,
            isJoin: data.g4,
          },
          {
            id: 5,
            isJoin: data.g5,
          }
        ]
        if(data.g1==0&&data.g2==0&&data.g3==0&&data.g4==0&&data.g5==0){
          wx.setStorageSync('isNewUser','1')
        }
        this.setData({
          locationList:locationList
        })
        //this.setNextBtn()
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('触发方法')
    this.getUserData()
    const openId = wx.getStorageSync('openid')
    wx.request({
      url: `${config.api_blink_url}/intel/upuser?openid=${openId}&g0=1`,
      method: "post",
      header: {
        'content-type': 'application/json'
      },
      success: (res) => {
        console.log(res)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log('触发方法1')
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})